﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Gasco.DataAccess;
using Gasco.Business.Services;
using Mani.Common;

public partial class Controls_contact_add : System.Web.UI.UserControl
{
    protected void Page_Init(object sender, EventArgs e)
    {
        ddBirthDateMonth.Items.Add(new ListItem("فرودين", "1"));
        ddBirthDateMonth.Items.Add(new ListItem("ارديبهشت", "2"));
        ddBirthDateMonth.Items.Add(new ListItem("خرداد", "3"));
        ddBirthDateMonth.Items.Add(new ListItem("تير", "4"));
        ddBirthDateMonth.Items.Add(new ListItem("مرداد", "5"));
        ddBirthDateMonth.Items.Add(new ListItem("شهريور", "6"));
        ddBirthDateMonth.Items.Add(new ListItem("مهر", "7"));
        ddBirthDateMonth.Items.Add(new ListItem("آبان", "8"));
        ddBirthDateMonth.Items.Add(new ListItem("آذر", "9"));
        ddBirthDateMonth.Items.Add(new ListItem("دي", "10"));
        ddBirthDateMonth.Items.Add(new ListItem("بهمن", "11"));
        ddBirthDateMonth.Items.Add(new ListItem("اسفند", "12"));

        for (int i = 1; i <= 31; i++)
        {
            ddBirthDateDay.Items.Add(new ListItem(i.ToString(), i.ToString()));
        }

    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] != null)
        {
            if (!Page.IsPostBack)
            {
                ContactService cs = new ContactService();
                contact c = cs.GetContactById(Convert.ToInt16(Request.QueryString["id"]));
                tbName.Text = c.name;
                tbIdentityID.Text = c.personal_contact.identity_id;
                tbNationalCode.Text = c.personal_contact.national_code;

                if (c.personal_contact.birthday.HasValue)
                {
                    string[] birth = c.personal_contact.birthday.Value.ToJalali().Split('/');
                    tbBirthDateYear.Text = birth[0];
                    ddBirthDateMonth.SelectedValue = (Convert.ToInt16(birth[1])).ToString();
                    ddBirthDateDay.SelectedValue = (Convert.ToInt16(birth[2])).ToString();
                }

                tbFatherName.Text = c.personal_contact.father_name;
                tbWorkTel.Text = c.personal_contact.work_tel;
                tbHomeTel.Text = c.personal_contact.home_tel;
                tbMobile.Text = c.personal_contact.mobile;
                tbAccountInfo.Text = c.account_info;
                tbEmail.Text = c.email;
                tbFax.Text = c.fax;
                tbAddress.Text = c.address;
                tbActivityField.Text = c.activity_field;
            }
        }

    }


    protected void btnclose_Click(object sender, EventArgs e)
    {
        InsertUpdateInfo();
        Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script1", "<script>pageClose();</script>");
    }

   

    private void InsertUpdateInfo()
    {
        bool InsertMode = (Request.QueryString["id"] == null);
        ContactService contactService = new ContactService();
        contact c;

        if (InsertMode)
        {
            c = new contact();
            c.personal_contact = new personal_contact();
        }
        else
        {
            c = contactService.GetContactById(Convert.ToInt32(Request.QueryString["id"]));
        }

        //Contact Info
        c.address = tbAddress.Text;
        c.account_info = tbAccountInfo.Text;
        c.email = tbEmail.Text;
        c.fax = tbFax.Text;
        c.name = tbName.Text;
        c.contact_type_id = 1;
        c.activity_field = tbActivityField.Text;
        c.update_date = DateTime.Now;

        //Personal Contact Info
        c.personal_contact.identity_id = tbIdentityID.Text;
        c.personal_contact.national_code = tbNationalCode.Text;

        if (String.IsNullOrEmpty(tbBirthDateYear.Text))
            c.personal_contact.birthday = null;
        else
        {
            string birthday = String.Format("{0}/{1}/{2}", tbBirthDateYear.Text, ddBirthDateMonth.SelectedValue, ddBirthDateDay.SelectedValue);
            c.personal_contact.birthday = birthday.ToDateTime();
        }

        c.personal_contact.father_name = tbFatherName.Text;
        c.personal_contact.home_tel = tbHomeTel.Text;
        c.personal_contact.work_tel = tbWorkTel.Text;
        c.personal_contact.mobile = tbMobile.Text;
        c.personal_contact.update_date = DateTime.Now;

        if (InsertMode)
        {
            c.create_date = DateTime.Now;
            c.personal_contact.create_date = DateTime.Now;
            contactService.CreateContact(c);
        }
        else
        {
            contactService.UpdateContact(c);
        }
    }
}
